﻿@model Management.Core.Role

@{
    ViewBag.Title = "编辑";
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.RoleID)

        <div class="form-group">
            @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name)
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-2">控制器权限</label>
            <div class="col-md-10">
                @{
                    int cnt = 0;
                    List<string> controllers = ViewBag.controllers;
                    foreach (var controller in controllers)
                    {
                        if (cnt++ % 5 == 0)
                        {
                            <p></p>
                        }
                        <input type="button" class="btn btn-info btn-group-sm" value="@controller" id="@controller" onclick="getAction(this.value)"/>
                        @*if (hasControllers.Contains(controller))
                        {
                            <input type="checkbox" name="SelectController" value="@controller" id="@controller" onchange="getAction(this.value, this.id)" checked/>@controller
                        }
                        else
                        {
                            <input type="checkbox" name="SelectController" value="@controller" id="@controller" onchange="getAction(this.value, this.id)"/>@controller
                        }*@
                    }
                }
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-md-2">动作权限</label>
            <div class="col-md-10" id="div_acions">
                
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                @*<input type="submit" value="保存" class="btn btn-default" />*@
                <input id="btnSave" type="button" value="保存" class="btn btn-default" onclick="save()"/>
            </div>
        </div>

        <div class="form-group">
            <ul id="treeDemo" class="ztree"></ul>
        </div>
    </div>
}

<div>
    @Html.ActionLink("返回列表", "Index")
</div>
<script type="text/html" id="actionTpl">
    {{# if(d["_actions"].length > 0){ }}
    <input type="button" value="全选" name="checkall" id="checkAll" onclick="checkedall()" class="btn btn-warning btn-group-xs"/>
        {{# for(var i = 0; i < d["_actions"].length; i++){ }}
            {{# if(i % 5 == 0) { }}
                <br />
            {{# } }}
            {{# if(d['_permissions'].indexOf(d['_actions'][i].PermissionID) >= 0 ) { }}
                <input type="checkbox" name="SelectAction" value="{{ d['_actions'][i].action }}" id="{{ d['_actions'][i].PermissionID }}" onchange="getPermissionid(this.id)" checked="checked"/> 
                {{ d['_actions'][i].Action }}
            {{# } }} 
            {{# if(d['_permissions'].indexOf(d['_actions'][i].PermissionID) < 0 ) { }}
                <input type="checkbox" name="SelectAction" value="{{ d['_actions'][i].action }}" id="{{ d['_actions'][i].PermissionID }}" onchange="getPermissionid(this.id)"/>
                {{ d['_actions'][i].Action }}
            {{# } }}
        {{# } }}
    {{# } }}
</script>

<script type="text/javascript">
    var roleid = $("#RoleID").val();
    var permissions = new Array();
    var flag = 0;//
    $(document).ready(function () {        
        getAction("", $("#RoleID").val());
    })
    
    Array.prototype.indexOf = function (val) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == val) return i;
        }
        return -1;
    };
    Array.prototype.remove = function (val) {
        var index = this.indexOf(val);
        if (index > -1) {
            this.splice(index, 1);
        }
    };    
    function getAction(value, id) {
        $.ajax({
                url: '@Url.Action("getAction")',
                type: 'post',
                data: { "name": value, "roleid": roleid },
                dataType: 'json',
                success: function (data) {
                    var tpl = document.getElementById("actionTpl").innerHTML;
                    laytpl(tpl).render(data, function (html) {
                        document.getElementById("div_acions").innerHTML = html;
                    })
                    if (data["_permissions"].length > 0 && flag == 0)
                    {                        
                        for(var i = 0;i < data["_permissions"].length; i++)
                        {
                            permissions.push(data["_permissions"][i]);//先填上原有的权限
                        }
                        flag = 1;//标识已有权限列表只加载一次
                    }
                },
                error: function (data) {
                    layer.alert("网络错误");
                }
            })
    }

    function getPermissionid(id) {
        if ($("#" + id).prop('checked')) {
            $("#" + id).prop("checked", true);
            permissions.push(parseInt(id));
        }
        else {
            $("#" + id).prop('checked', false);
            permissions.remove(parseInt(id));
        }
    }

    function checkedall() {
        $("input[name='SelectAction']").each(function () {
            if ($(this).prop('checked')) {
                $(this).prop("checked", false);
                permissions.remove(parseInt(this.id));
            }
            else {
                $(this).prop('checked', true);
                permissions.push(parseInt(this.id));
            }
        })
    }

    function uniquePermission(array) {
        var res = [];
        var json = {};
        for (var i = 0; i < array.length; i++) {
            if (!json[array[i]]) {
                res.push(array[i]);
                json[array[i]] = 1;
            }
        }
        return res;
    }

    function save(){
        var name = document.getElementById("Name").value;
        var selectRoles = uniquePermission(permissions);
        $.ajax({
            url: '@Url.Action("Edit")',
            data: { "id": roleid,"name":name, "selectedRoles": selectRoles },
            type: 'post',
            datatype: 'json',
            success: function (data) {
                layer.alert("success");
            },
            error:function()
            {
                layer.alert("网络错误");
            }
        })
    }
    
</script>
@Scripts.Render("~/bundles/jqueryval")